/*
 * @Descripttion:
 * @version:
 * @Author: WangMingJun
 * @Date: 2021-10-07 12:09:43
 * @LastEditors: WangMingJun
 * @LastEditTime: 2024-06-27 16:25:52
 */
import axios from 'axios'
import store from '../store/index'

export function request(config) {
  // 1.创建axios的实例
  const instance = axios.create({
    baseURL: 'http://msearchcdn.kugou.com/api/v3',
    timeout: 5000,
  })

  // 2.axios的拦截器
  // 2.1.请求拦截的作用
  instance.interceptors.request.use(
    (config) => {
       if (store.state.token) {
          // 将token设置在headers ['Authorization'] 上面
          config.headers['Authorization'] = `Bearer ${store.state.token}`
        }
      return config
    },
    (err) => {
      console.log(err)
    }
  )

  // 2.2.响应拦截
  instance.interceptors.response.use(
    (res) => {
      return res.data
    },
    (err) => {
      console.log(err)
    }
  )

  // 3.发送真正的网络请求
  return instance(config)
}
